/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.models;

import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author Calimeraw93
 */
public class EditInfoValidator implements Validator {

    private MessageSource messageSource;
    
    public EditInfoValidator(MessageSource msgSrc)
    {
        super();
        this.messageSource = msgSrc;
    }
    
    @Override
    public boolean supports(Class<?> type) {
        return UserInfoData.class.isAssignableFrom(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        try
        {
            final String ERRORS1 = this.messageSource.getMessage("register.errors.empty", null, Locale.FRENCH);
            final String ERRORS2 = this.messageSource.getMessage("register.errors.password", null, Locale.FRENCH);
            final String ERRORS3 = this.messageSource.getMessage("register.errors.mail", null, Locale.FRENCH);
    

            UserInfoData uid = (UserInfoData)o;

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "firstname", 
                                                      "errors.required",
                                                      new Object[] {"firstname"},
                                                      ERRORS1);

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "lastname", 
                                                      "errors.required",
                                                      new Object[] {"lastname"},
                                                      ERRORS1);

            if (!uid.getPassword().isEmpty()) {
                ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                          "password", 
                                                          "errors.required",
                                                          new Object[] {"password"},
                                                          ERRORS1);

                ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                          "pwd_verif", 
                                                          "errors.required",
                                                          new Object[] {"pwd_verif"},
                                                          ERRORS1);

                if (!uid.isPasswordValid())
                {
                    errors.rejectValue("pwd_verif",
                                       "errors.required",
                                        new Object[] {"pwd_verif"},
                                        ERRORS2);
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Validator error :" + e);
        }
    }
    
}
